package com.fanhang.samples.spring.sql2o.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

import com.fanhang.samples.spring.sql2o.common.Sql2oDaoSupport;
import com.fanhang.samples.spring.sql2o.dao.RoleDao;
import com.fanhang.samples.spring.sql2o.po.Role;

import static com.google.common.collect.ImmutableMap.*;

@Repository
public class RoleDaoImpl extends Sql2oDaoSupport implements RoleDao {

	@Override
	public Role get(Long id) {
		String sql = "SELECT * FROM stat_role WHERE id = :id";
		return super.getSessionTemplate().fetchFirst(sql, of("id", id), Role.class);
	}
	

	@Override
	public List<Role> getList(Role model) throws DataAccessException {
		StringBuilder sql = new StringBuilder("SELECT * FROM stat_role WHERE 1=1 ");
		if (model.getParentId() != null) {
			sql.append(" and parent_id = :parentId ");
		}
		if (model.getAreaId() != null) {
			sql.append(" and area_id = :areaId ");
		}
		if (!StringUtils.isEmpty(model.getName())) {
			sql.append(" and name like '%:name%' ");
		}
		return super.getSessionTemplate().fetch(sql, describe(model), Role.class);
	}
	

	@Override
	public int save(Role model) {
		String sql = "insert into stat_user(id,login_name,password,user_name,role_id,status,creator,create_time) values (:id, :loginName, :password, :userName, :roleId, :status, :creator, :createTime)";
		return super.getSessionTemplate().update(sql, describe(model));
	}

	@Override
	public int delete(Role model) {
		String sql = "update employee set name=:name,birthday=:birthday,status=:status WHERE id = :id";
		return super.getSessionTemplate().update(sql, describe(model));
	}

	@Override
	public int update(Role model) {
		String sql = "update employee set name=:name,birthday=:birthday,status=:status WHERE id = :id";
		return super.getSessionTemplate().update(sql, describe(model));
	}
}
